package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopRemark;
import com.myapp.ruyixing.model.ShopRemark.ShopRemarkItem;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.CompatibilityUtils;

public class ShopRemarkListActivity extends BaseActivity implements
		OnItemClickListener {

	private ListView mListView;
	private TextView mEmptyView;

	private String mShopId;
	private String mShopName;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ShopRemark data = (ShopRemark) o;

			if (data != null && data.items != null) {
				MyListAdapter adapter = new MyListAdapter(data.items);
				mListView.setAdapter(adapter);
			}
			mListView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_remark_list);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);

		mShopId = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_ID);
		mShopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);
		if (TextUtils.isEmpty(mShopId) == true) {
			Toast.makeText(getApplicationContext(), "商户为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			RYHttpClient.getShopRemarkList(this, mHandler, mShopId);
		}

		if (TextUtils.isEmpty(mShopName) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("点评列表\n"
					+ mShopName);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	private void initView() {

		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(this);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(
								ShopRemarkListActivity.this,
								ShopAddRemarkActivity.class).putExtra(
								ShopDetailActivity.INTENT_SHOP_ID, mShopId)
								.putExtra(ShopDetailActivity.INTENT_SHOP_NAME,
										mShopName));

						finish();
					}
				});
	}

	private class ViewHolder {
		TextView mUserTextView;
		TextView mRemarkView;
		TextView mTimeView;
		ImageView mScoreView;
		TextView mPriceView;
		ImageView mExpandView;
	}

	private class MyListAdapter extends ArrayAdapter<ShopRemarkItem> {

		public MyListAdapter(List<ShopRemarkItem> list) {
			super(ShopRemarkListActivity.this, -1, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_shop_remark_list, parent, false);

				final ViewHolder holder = new ViewHolder();

				holder.mRemarkView = (TextView) convertView
						.findViewById(R.id.remark);
				holder.mExpandView = (ImageView) convertView
						.findViewById(R.id.remark_expand);
				holder.mExpandView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						holder.mRemarkView.setMaxLines(Integer.MAX_VALUE);
						holder.mExpandView.setVisibility(View.GONE);
					}
				});
				holder.mUserTextView = (TextView) convertView
						.findViewById(R.id.user);
				holder.mScoreView = (ImageView) convertView
						.findViewById(R.id.score);
				holder.mPriceView = (TextView) convertView
						.findViewById(R.id.price);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);

				ViewTreeObserver vto = holder.mRemarkView.getViewTreeObserver();
				vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						ViewTreeObserver observer = holder.mRemarkView
								.getViewTreeObserver();
						CompatibilityUtils.removeOnGlobalLayoutListener(
								observer, this);

						if (holder.mRemarkView.getLineCount() > 2) {
							holder.mExpandView.setVisibility(View.VISIBLE);
							holder.mRemarkView.setMaxLines(2);
						} else {
							holder.mExpandView.setVisibility(View.GONE);
						}
					}
				});

				convertView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			ShopRemarkItem item = getItem(position);

			holder.mUserTextView.setText(item.username);
			holder.mPriceView.setText(item.price);
			holder.mRemarkView.setText(item.content);
			holder.mTimeView.setText(item.datetime);
			holder.mScoreView.setImageLevel(item.score);

			return convertView;

		}
	}
}
